package com.cqc.web;

import com.cqc.entity.UserEB;
import com.cqc.kafka.KafkaUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author cui
 * @date 2025/5/14 22:38
 * @description
 **/

@RestController
@RequestMapping("/provider/kafka/")
public class KafkaController {
    @Autowired
    KafkaUtil kafkaUtil;


    /**
     * http://localhost:8080/provider/kafka/send1
     */
    @GetMapping("send1")
    public ResponseEntity<String> send1(Long id) throws JsonProcessingException {
        UserEB userEB = this.initUser();
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(userEB);
        kafkaUtil.send("aud_log_query", json);
        return ResponseEntity.ok("成功");
    }

    /**
     * http://localhost:8080/provider/kafka/send2?id=1
     */
    @GetMapping("send2")
    public ResponseEntity<String> send2(Long id) throws JsonProcessingException {
        UserEB userEB = this.initUser();
        userEB.setId(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(userEB);
        kafkaUtil.send("aud_log_query", String.valueOf(id), json);
        return ResponseEntity.ok("成功");
    }


    private UserEB initUser() {
        UserEB userEB = new UserEB();
        userEB.setId(1L);
        userEB.setUserName("刘亦菲");
        userEB.setPassword("123456");
        userEB.setName("lyf");
        userEB.setAge(20);
        userEB.setSex(0);
        userEB.setBirthday(new Date());
        userEB.setCreated(new Date());
        userEB.setUpdated(new Date());
        return userEB;
    }
}
